package com.qf.fmw.service.impl;

import com.qf.fmw.dao.UserDao;
import com.qf.fmw.dao.impl.UserDaoImpl;
import com.qf.fmw.entity.bo.MessageBo;
import com.qf.fmw.entity.bo.UserBo;
import com.qf.fmw.entity.po.User;
import com.qf.fmw.entity.vo.RespResult;
import com.qf.fmw.service.UserService;
import com.qf.fmw.utils.MD5Utils;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    // 登录
    @Override
    public RespResult login(HttpSession session, String name, String password) {
        String encryptedPassword = MD5Utils.md5(password);
        Integer adminId = userDao.selectId(name, encryptedPassword);
        if (adminId == null) {
            return RespResult.respError(null, null);
        }
        Integer roleId = userDao.selectRoleId(adminId);
        session.setAttribute("adminId", adminId);
        session.setAttribute("roleId", roleId);
        return RespResult.respSuccess(null, adminId);
    }


    // 注册
    @Override
    public RespResult register(String name, String password, String email, String sex) {
        if (userDao.isUsernameExists(name)) {
            return RespResult.respError("用户名已存在，请尝试使用其他用户名", null);
        }
        String encryptedPassword = MD5Utils.md5(password);
        boolean isRegistered = userDao.registerUser(name, encryptedPassword, email, sex);
        if (isRegistered) {
            return RespResult.respSuccess(null, null);
        } else {
            return RespResult.respError("注册失败，请稍后重试", null);
        }
    }


    /*
        注销当前用户会话
     */
    @Override
    public RespResult logout(HttpSession session) {
        // 移除会话中的管理员ID和角色ID属性，实现用户注销功能
        session.removeAttribute("adminId");
        session.removeAttribute("roleId");
        // 返回操作成功的响应结果
        return RespResult.respSuccess(null, null);
    }

    @Override
    public RespResult showAllUsers() {
        List<UserBo> userList = userDao.selectAllUsers();
        if (userList == null || userList.isEmpty()) {
            return RespResult.respError("未找到会员用户", null);
        }
        return RespResult.respSuccess("找到会员用户", userList);
    }

    //展示用户个人信息
    @Override
    public RespResult showUserMessage(int userId) {
        List<MessageBo> user = userDao.selectUserMessage(userId);
        if (user == null) {
            return RespResult.respError("未找到用户信息", null);
        }
        return RespResult.respSuccess("找到用户信息", user);
    }

    //修改个人信息
    @Override
    public RespResult updateUserMessage(int userId, String userName, String email, String sex) {
        boolean updated = userDao.updateUserMessage(userId, userName, email, sex);
        if (updated) {
            return RespResult.respSuccess("用户信息更新成功", null);
        } else {
            return RespResult.respError("用户信息更新失败", null);
        }
    }

    //修改密码
    @Override
    public RespResult updateUserPassword(int userId, String newPassword) {
        boolean updated = userDao.updateUserPassword(userId, newPassword);
        if (updated) {
            return RespResult.respSuccess("密码更新成功", null);
        } else {
            return RespResult.respError("密码更新失败", null);
        }
    }


    @Override
    public RespResult showUserBySex(String sex) {
        List<UserBo> userList = userDao.selectUserBySex(sex);
        if (userList == null || userList.isEmpty()) {
            return RespResult.respError("未找到符合条件的用户", null);
        }
        return RespResult.respSuccess("找到符合条件的用户", userList);
    }

    @Override
    public RespResult showUserByName(String name) {
        List<UserBo> userList = userDao.selectUserByName(name);
        if (userList == null || userList.isEmpty()) {
            return RespResult.respError("未找到符合条件的用户", null);
        }
        return RespResult.respSuccess("找到符合条件的用户", userList);
    }

    @Override
    public RespResult deleteUser(int userId) {
        Integer deleted = userDao.deleteUser(userId);
        if (deleted == 0) {
            return RespResult.respError("删除失败", null);
        }
        return RespResult.respSuccess("删除成功", null);
    }


    @Override
    public RespResult showUserByNameAndSex(String name, String sex) {
        List<UserBo> userList = userDao.selectUserByNameAndSex(name, sex);
        if (userList == null || userList.isEmpty()) {
            return RespResult.respError("未找到符合条件的用户", null);
        }
        return RespResult.respSuccess("找到符合条件的用户", userList);
    }


    @Override
    public RespResult showInvalidUsers() {
        List<UserBo> invalidUserList = userDao.selectInvalidUsers();
        if (invalidUserList == null || invalidUserList.isEmpty()) {
            return RespResult.respError("未找到无效会员用户", null);
        }
        return RespResult.respSuccess("找到无效会员用户", invalidUserList);
    }

    @Override
    public RespResult searchInvalidUsers(String query) {
        List<UserBo> invalidUserList = userDao.searchInvalidUsers(query);
        if (invalidUserList == null || invalidUserList.isEmpty()) {
            return RespResult.respError("未找到符合条件的无效会员用户", null);
        }
        return RespResult.respSuccess("找到符合条件的无效会员用户", invalidUserList);
    }


    @Override
    public RespResult deleteInvalidUser(int userId) {
        Integer deleted = userDao.deleteInvalidUser(userId);
        if (deleted == 0) {
            return RespResult.respError("删除失败", null);
        }
        return RespResult.respSuccess("删除成功", null);
    }

    @Override
    public RespResult getAddress(int userId) {
        return null;
    }

    @Override
    public RespResult searchInvalidUser(String query) {
        List<UserBo> invalidUserList = userDao.searchInvalidUsers(query);
        if (invalidUserList == null || invalidUserList.isEmpty()) {
            return RespResult.respError("未找到符合条件的无效会员用户", null);
        }
        return RespResult.respSuccess("找到符合条件的无效会员用户", invalidUserList);
    }

    @Override
    public RespResult searchInvalidUserBySex(String gender) {
        List<UserBo> invalidUserList = userDao.selectInvalidUserBySex(gender);
        if (invalidUserList == null || invalidUserList.isEmpty()) {
            return RespResult.respError("未找到符合条件的无效会员用户", null);
        }
        return RespResult.respSuccess("找到符合条件的无效会员用户", invalidUserList);
    }

}
